

package com.hazelcast.json.internal;

import com.hazelcast.internal.serialization.DataSerializerHook;
import com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory;
import com.hazelcast.internal.serialization.impl.FactoryIdHelper;
import com.hazelcast.nio.serialization.DataSerializableFactory;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;

import java.util.function.Supplier;

import static com.hazelcast.internal.serialization.impl.FactoryIdHelper.JSON_DS_FACTORY;
import static com.hazelcast.internal.serialization.impl.FactoryIdHelper.JSON_DS_FACTORY_ID;

/**
 * Serialization hook for JSON classes.
 */
@SuppressWarnings("checkstyle:JavadocVariable")
public final class JsonDataSerializerHook implements DataSerializerHook {

    public static final int F_ID = FactoryIdHelper.getFactoryId(JSON_DS_FACTORY, JSON_DS_FACTORY_ID);

    public static final int JSON_SCHEMA_NAME_VALUE = 0;
    public static final int JSON_SCHEMA_TERMINAL_NODE = 1;
    public static final int JSON_SCHEMA_STRUCT_NODE = 2;

    private static final int LEN = JSON_SCHEMA_STRUCT_NODE + 1;

    @Override
    public int getFactoryId() {
        return F_ID;
    }

    @Override
    public DataSerializableFactory createFactory() {
        //noinspection unchecked
        Supplier<IdentifiedDataSerializable>[] constructors = new Supplier[LEN];

        constructors[JSON_SCHEMA_NAME_VALUE] = JsonSchemaNameValue::new;
        constructors[JSON_SCHEMA_TERMINAL_NODE] = JsonSchemaTerminalNode::new;
        constructors[JSON_SCHEMA_STRUCT_NODE] = JsonSchemaStructNode::new;

        return new ArrayDataSerializableFactory(constructors);
    }

}
